package com.bl.commons.controller;

import ch.qos.logback.core.net.SyslogConstants;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.NumberUtil;
import com.bl.commons.constants.SysConstants;
import com.bl.commons.models.Result;
import com.bl.commons.utils.EasyexcelUtils;
import com.bl.commons.utils.HutoolUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 健康测试控制器
 *
 * @author bl
 * @date 2023/10/16
 */
@RestController
@RequestMapping(value = "/test")
@Api(value = "一些测试",tags = "一些测试")
public class HealthController {

    @GetMapping(value = "health")
    @ApiOperation(value = "健康检测")
    public Result<Object> health(){
        return Result.success(null,"service is alive ...");
    }



    @PostMapping("/export-poi")
    @ApiOperation(value = "excel导出测试")
    public void exportExcel(HttpServletResponse response, @RequestBody Map<String,Object> object) throws IOException{
        int testCounts = (int) object.get("testCounts");
        List<Map<String,Object>> datas = new ArrayList<>();
        for (int i = 1; i < testCounts; i++) {
            Map<String,Object> map = new HashMap<>();
            map.put("id",i);
            map.put("name","张"+i);
            map.put("age",i);
            map.put("sex", NumberUtil.isEven(i)?"男":"女");
            datas.add(map);
        }
        Map<String,String> headAlias = new HashMap<>();
        headAlias.put("name","姓名");
        headAlias.put("age","年龄");
        headAlias.put("sex","性别");
        DateTime start = DateUtil.date(DateUtil.current());
        System.out.println("poi导出开始===="+ DateUtil.format(start,SysConstants.TIME_FORMAT_SSS));
        HutoolUtils.dynamicExport("poi导出",datas,headAlias,response,object.get("excelType").toString());
        DateTime end = DateUtil.date(DateUtil.current());
        System.out.println("poi导出结束===="+ DateUtil.format(end,SysConstants.TIME_FORMAT_SSS)+"\npoi导出"+testCounts+"条数据总耗时==="+DateUtil.between(start,end, DateUnit.MS));
    }

    @PostMapping("/export-easyexcel")
    @ApiOperation(value = "excel导出测试")
    public void exportEasyexcel(HttpServletResponse response, @RequestBody Map<String,Object> object) throws IOException {
        int testCounts = (int) object.get("testCounts");
        //表头
        List<List<String>> headers = new ArrayList<>();
        List<String> head1 = new ArrayList<>();
        List<String> head2 = new ArrayList<>();
        List<String> head3 = new ArrayList<>();
        List<String> head4 = new ArrayList<>();
        head1.add("id");
        head2.add("姓名");
        head3.add("年龄");
        head4.add("性别");
        headers.add(head1);
        headers.add(head2);
        headers.add(head3);
        headers.add(head4);
        //数据
        List<List<Object>> datas = new ArrayList<>();
        for (int i = 1; i < testCounts; i++) {
            List<Object> obj = new ArrayList<>();
            obj.add(i);
            obj.add("张"+i);
            obj.add(i);
            obj.add(NumberUtil.isEven(i)?"男":"女");
            datas.add(obj);
        }
        DateTime start = DateUtil.date(DateUtil.current());
        System.out.println("easyExcel导出开始===="+ DateUtil.format(start,SysConstants.TIME_FORMAT_SSS));
        EasyexcelUtils.dynamicExport(response,headers,datas,object.get("excelType").toString(),"easyExcel导出");
        DateTime end = DateUtil.date(DateUtil.current());
        System.out.println("easyExcel导出结束===="+ DateUtil.format(end,SysConstants.TIME_FORMAT_SSS)+"\neasyExcel导出"+testCounts+"条数据总耗时==="+DateUtil.between(start,end, DateUnit.MS));
    }
}
